﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Linq.Expressions;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;
using XYAC.Framework.MonitorTireMaterialOnline.IServices;
using XYAC.Framework.MonitorTireMaterialOnline.LocalData.Context;
using XYAC.Framework.MonitorTireMaterialOnline.LocalData.Models;
using XYAC.Framework.MonitorTireMaterialOnline.OracleRemoteDB;

namespace XYAC.Framework.MonitorTireMaterialOnline.Services
{
    public class YB_LOT_INSPECT_LocalService : ServiceBase<int, LocalDataContext>, IYB_LOT_INSPECT_LocalService
    {
        private const string __REMOTE_SYNC_STATE_PENDING = "PENDING";
        private const string __REMOTE_SYNC_STATE_COMPLETE = "COMPLETE";
        private const string __REMOTE_SYNC_STATE_PLANED = "PLANED";

        public YB_LOT_INSPECT_LocalService(IOptionsMonitor<AppConfig> appConfig, ILogger<YB_LOT_INSPECT_LocalService> logger, IServiceProvider provider) : base(appConfig, logger, provider)
        {
            this.AppConfig = appConfig;
            this.provider = provider;
            this.Logger = logger;
        }
        public IOptionsMonitor<AppConfig> AppConfig { get; }

        private IServiceProvider provider;

        //public LocalDataContext Db { get; }
        public ILogger Logger { get; }

        internal IEnumerable<YB_LOT_INSPECT_Local> GetFailedData()
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<LocalDataContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PLANED && T.RemoteSyncState == false).ToList();
                return snList;
            }
        }

        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNoList"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal IEnumerable<LocalData.Models.YB_LOT_INSPECT_Local> GetSnData(string[] machineNoList, string state = __REMOTE_SYNC_STATE_PENDING)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<LocalDataContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
                return snList;
            }
        }






        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNo"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        internal IEnumerable<LocalData.Models.YB_LOT_INSPECT_Local> GetSnData(string machineNo, string state = __REMOTE_SYNC_STATE_PENDING)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<LocalDataContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNo == T.EQP && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
                return snList;
            }
        }

        /// <summary>
        /// 获取本地数据状态为__REMOTE_SYNC_STATE_COMPLETE的数据
        /// </summary>
        /// <param name="machineNo"></param>
        /// <param name="plandate"></param>
        /// <returns></returns>
        internal IEnumerable<LocalData.Models.YB_LOT_INSPECT_Local> GetSnData(string? machineNo, DateTime plandate)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<LocalDataContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNo == T.EQP && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_COMPLETE && T.PLAN_DATE.Value.Date == plandate).ToList();
                return snList;
            }
        }

        internal int UpdateFlag(string rEMOTE_SYNC_STATE_COMPLETE, OracleRemoteDB.YB_LOT_INSPECT currentNotifyData)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<LocalDataContext>();
                int ret = 0;
                using (var trans = Db.Database.BeginTransaction())
                {
                    try
                    {
                        currentNotifyData.RECEIVE_FLAG = rEMOTE_SYNC_STATE_COMPLETE;
                        Db.Update(currentNotifyData);
                        var tempRet = Db.SaveChanges();
                        trans.Commit();
                        ret = tempRet;
                    }
                    catch (Exception ex)
                    {
                        trans.Rollback();
                        this.Logger.LogError("更新本地缓存中间表数据失败", ex);
                    }
                }
                return ret;
            }
        }



        /// <summary>
        /// 返回当前状态的PlanNo和Sn数据,获取最新条码数据
        /// </summary>
        /// <param name="machineNoList"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IEnumerable<YB_LOT_INSPECT> GetSnData(params string[] machineNoList)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<OracleMesContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING).ToList();
                return snList;
            }
        }

        public IEnumerable<YB_LOT_INSPECT> GetSnData(DateTime plandate, params string[] machineNoList)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<OracleMesContext>();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_PENDING && T.PLAN_DATE.Value.Date == plandate).ToList();
                return snList;
            }
        }

        IEnumerable<YB_LOT_INSPECT_Local> IYB_LOT_INSPECT_LocalService.GetSnData(DateTime plandate, params string[] machineNoList)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<LocalDataContext>();
                //var snList = Db.YB_LOT_INSPECT.ToList();
                //var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_COMPLETE && T.PLAN_DATE.Value.Date == plandate).ToList();
                var snList = Db.YB_LOT_INSPECT.Where(T => machineNoList.Contains(T.EQP) && T.RECEIVE_FLAG == __REMOTE_SYNC_STATE_COMPLETE && T.PLAN_DATE == plandate).ToList();
                return snList;

            }
        }


        public IEnumerable<YB_LOT_INSPECT_Local> GetSnPage(int pageIndex = 0, int rowCount = 50, Expression<Func<YB_LOT_INSPECT_Local, bool>> filterCondition = null, Expression<Func<YB_LOT_INSPECT_Local, object>> orderByCondition = null, Expression<Func<YB_LOT_INSPECT_Local, object>> orderByDescCondition = null)
        {
            using (var scope = this.Provider.CreateScope())
            {
                var Db = scope.ServiceProvider.GetService<OracleMesContext>();
                IQueryable<YB_LOT_INSPECT_Local> data = Db.Set<YB_LOT_INSPECT_Local>();
                if (filterCondition != null)
                    data = data.Where(filterCondition);
                if (orderByCondition != null)
                    data = data.OrderBy(orderByCondition);

                if (orderByDescCondition != null)
                    data = data.OrderBy(orderByDescCondition);
                return data.ToList();
            }
        }
    }
}
